//
//  articlemanager.m
//  myapplication
//
//  Created by Paolo Marcolli on 14/01/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "articlemanager.h"
#import "article.h"

@implementation articlemanager
@synthesize list;

// Load data from database.
-(void)load
{

    NSString *docsDir;
    NSArray *dirPaths;
    
    // Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(
                                                   NSDocumentDirectory, NSUserDomainMask, YES);
    
    docsDir = [dirPaths objectAtIndex:0];
    
    // Build the path to the database file
    databasePath = [[NSString alloc] 
                    initWithString: [docsDir stringByAppendingPathComponent: 
                                     @"polosoft.db"]];
    
    NSFileManager *filemgr = [NSFileManager defaultManager];
    
    if ([filemgr fileExistsAtPath: databasePath ] == NO)
    {
        const char *dbpath = [databasePath UTF8String];
        
        if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
        {
            char *errMsg;
            const char *sql_stmt = 
            "CREATE TABLE budget (idx integer NOT NULL PRIMARY KEY AUTOINCREMENT,code text,description text,price float,imageURL text)";
            
            if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
                sqlite3_close(contactDB);
        } 
    }
    
    /* Now here we have to write the code from the database to display the details */
	list = [[NSMutableArray alloc] init];
    
    const char *dbpath = [databasePath UTF8String];
    sqlite3_stmt    *statement;
    
    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
    {
        NSString *querySQL = [NSString stringWithFormat: 
                              @"SELECT code,description,price,imageURL FROM budget"];
        
        const char *query_stmt = [querySQL UTF8String];
        
        if (sqlite3_prepare_v2(contactDB,query_stmt, -1, &statement, NULL) == SQLITE_OK)
        {
            while(sqlite3_step(statement) == SQLITE_ROW)
            {
                // Read the data from the result row
				NSString *aCode = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
				NSString *aDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
				NSNumber *aPrice = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
                NSString *aImageURL = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
                
				// Create a new animal object with the data from the database
				article *item = [[article alloc] initWithName:aCode description:aDescription url:aImageURL price:aPrice];
                
				// Add the animal object to the animals Array
				[list addObject:item];
                
				item=nil;
                
            }
            sqlite3_finalize(statement);
        }
        sqlite3_close(contactDB);
    }
}

// Add new data in list and in database.
-(void)add:(NSString *)n description:(NSString *)d url:(NSString *)u price:(NSNumber *)p ;
{

    // Create a new animal object with the data from the database
    article *item = [[article alloc] initWithName:n description:d url:u price:p];
    
    // Add the animal object to the animals Array
    [list addObject:item];
    
    sqlite3_stmt    *statement;
    const char *dbpath = [databasePath UTF8String];
    
    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
    {
        NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO budget (code, description, price,imageURL) VALUES (\"%@\", \"%@\", \"%@\", \"%@\")", [item code], [item description],[item price],[item imageURL]];
        
        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
        
        sqlite3_step(statement); //== SQLITE_DONE)
        
        sqlite3_finalize(statement);
        sqlite3_close(contactDB);
    } 

    item=nil;

}

@end
